<?php

namespace App\Models\Teaching;

use App\Models\File;
use App\Models\Files;
use App\Models\System\Scale;
use Illuminate\Database\Eloquent\Model;

class TeachingHomework extends Model
{
    protected $table = 'teaching_homework';
    protected $guarded = [];
    protected $appends = [
        'link_set',
        'score_info'
    ];

    public function live()
    {
        return $this->belongsTo(Live::class);
    }

    public function scale()
    {
        return $this->hasOne(Scale::class, 'id', 'scale_id');
    }

    public function more()
    {
        return $this->hasOne(HomeworkInfo::class, 'homework_id');
    }

    public function scaleMore()
    {
        return $this->hasOne(Scale::class, 'id', 'scale_id')->with('detail', 'tableDetail');
    }

    public function score()
    {
        return $this->hasMany(HomeworkScore::class, 'homework_id');
    }

    public function getScoreInfoAttribute(){
        $scaleMore = !empty($this->relations['scaleMore']) ? $this->relations['scaleMore'] : '';
        $homeworkScore = !empty($this->relations['score']) ? $this->relations['score'] : Null;
        if(!$scaleMore){
            return [];
        }

        $result = $this->toHomeworkTableData($scaleMore->tableDetail, $homeworkScore);

        return $result;
    }

    public function getLinkSetAttribute(){
        $linkId = !empty($this->attributes['link_id']) ? $this->attributes['link_id'] : '';

        if(!$linkId){
            return [];
        }

        $ids = explode(',', $linkId);
        $result = File::whereIn('id', $ids)->get();

        return $result;
    }



    /**
     * @Description: 处理量表数据形成合适的table数据
     * @Author: kzj
     * @datetime: 2022-11-26
     */
    public function toHomeworkTableData($detail, $scores = NULL)
    {
        $result = [];

        if($detail->count()) {
            foreach($detail as $d) {
                $tableData['id'] = $d->id;
                $tableData['pid'] = $d->pid;
                $tableData['top_id'] = $d->top_id;
                $tableData['scale_id'] = $d->scale_id;
                $tableData['score'] = $d->score;
                $tableData['desc'] = $d->desc;
                $tableData['self_rate'] = $d->self_rate;
                $tableData['reviewer'] = $d->reviewer;
                $tableData['review_name'] = $d->review_name;
                $tableData['level'] = $d->level;
                $tableData['tier'] = $d->tier;
                $tableData['min_score'] = $d->min_score;
                $tableData['max_score'] = $d->max_score;
                $tableData['module'] = $d->module;
                $tableData['module_text'] = $d->module_text;
                $tableData['scale_index'] = $d->scale_index;

                if($scores) {
                    $scoreData = $scores->where('detail_id', $d->id)->first();
                    $score = $scoreData ? $scoreData->score : 0;
                    $tableData['score'] = $score > 0 ? $score : '';
                }

                $title = $this->toDealData($d);

                foreach($title as $t => $v) {
                    $tableData[$t] = $v;
                }
                $result[] = $tableData;
            }
        } else {
            return [];
        }

        return $result;
    }

    /**
     * @Description: 递归处理数据
     * @Author: kzj
     * @datetime: 2022-11-26
     */
    public function toDealData($d, $result = [], $span = [])
    {
        $result['title_arr'][] = ['id' => $d->id, 'name' => $d->title, 'level' => $d->level];
        $result['title'.$d->level] = $d->title;

        if($d->child_count > 1) {
            $span[] = ['id' => $d->id, 'col' => $d->level];
        }

        if($d->parent) {
            return $this->toDealData($d->parent, $result, $span);
        }

        $result['span'] = $span;

        return $result;
    }

    public function setFilesIdAttribute($filesIdArr)
    {
        $this->files()->whereNotIn('id', $filesIdArr)->update(['ilk_type' => null, 'ilk_id' => null]);
        $this->files()->saveMany(Files::query()->whereIn('id', $filesIdArr)->get());
    }
}
